#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

const wregex mcWregexNegDecZh(L"(-?)(((\\d+)(\\.\\d+))|(\\.(\\d+)))");
const wregex mcWregexNegDecEn(L"(-?)((\\d+)(\\.\\d+))");
const wregex mcWregexNegDecWithIntegerZh(L"(-?)(((\\d+)(\\.\\d+))|(\\.(\\d+))|(\\d+))");
const wregex mcWregexNegDecWithIntegerEn(L"(-?)(((\\d+)(\\.\\d+))|(\\d+))");

const map<string, string> mcMapStrStrEnMonth = 
{
	{"jan", "january"},
	{"feb", "february"},
	{"mar", "march"},
	{"apr", "april"},
	{"may", "may"},
	{"jun", "june"},
	{"jul", "july"},
	{"aug", "august"},
	{"sept", "september"},
	{"sep", "september"},
	{"oct", "october"},
	{"nov", "november"},
	{"dec", "december"},
};

const vector<string> mcVecStrEnMonth = 
{
	"january", "february", "march", "april",
	"may", "june", "july", "august",
	"september", "october", "november", "december",
	"in"
};

// 负数or小数
void TextNormalizer::TnReNegDecNum(wstring* fpInWsentence, const bool fcInWithInteger, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while (regex_search(*fpInWsentence, tWsmatch, 
		fcInWithInteger ? (fcInLanguage==LANG_EN?mcWregexNegDecWithIntegerEn:mcWregexNegDecWithIntegerZh) : (fcInLanguage==LANG_EN?mcWregexNegDecEn:mcWregexNegDecZh) ))
	{
		string tStrNegDec = wstring2utf8string(tWsmatch[0]);		//

		//提取数字后面的字符串
		const vector<wstring> tcZhVecStrNextWords = GetWstrPositionNextWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_ZH);

		//提取数字前面的字符串
		const vector<wstring> tcZhVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_ZH);
		const vector<wstring> tcEnVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 2, LANG_EN);

		string tEnMonth = "";
		string tEn1Before = LastWordsConnectBeforeString(tcEnVecStrLastWords, 1, LANG_EN);
		transform(tEn1Before.begin(), tEn1Before.end(), tEn1Before.begin(), ::tolower);		//大写转小写
		if( mcMapStrStrEnMonth.find(tEn1Before) != mcMapStrStrEnMonth.end() ) {
			tEnMonth = mcMapStrStrEnMonth.at(tEn1Before);
		}
		if( mcVecStrEnMonth.end() != find(mcVecStrEnMonth.begin(), mcVecStrEnMonth.end(), tEn1Before) ) {
			tEnMonth = tEn1Before;
		}

		vector<string> tVecZhBefore;
		for(size_t i=0; i<=3; i++) {
			tVecZhBefore.push_back( LastWordsConnectBeforeString(tcZhVecStrLastWords, i, LANG_ZH) );
		}
		
		vector<string> tVecZhAfter;
		for(size_t i=0; i<=3; i++) {
			tVecZhAfter.push_back( NextWordsConnectAfterString(tcZhVecStrNextWords, i, LANG_ZH) );
		}

		string tStrReplace = "";

		if(tVecZhBefore[1]=="第" || tVecZhBefore[3]=="问题数") {
			tStrReplace += ReplaceMethod(vector<string>{tStrNegDec}, (fcInLanguage==LANG_EN)?RME_en_negative_decimal:RME_zh_negative_decimal);
			tStrReplace += " ";
		}
		else if(tVecZhAfter[3]=="定盘价" || tVecZhAfter[3]=="冬奥会" || tVecZhBefore[2]=="编号") {
			tStrReplace += ReplaceMethod(vector<string>{tStrNegDec}, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_digit_yi1);
		}
		else if(tVecZhAfter[2]=="来电") {
			tStrReplace += ReplaceMethod(vector<string>{tStrNegDec}, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_telephone);
		}
		else if(tStrNegDec=="123" && tVecZhBefore[2]=="广播") {
			tStrReplace += ReplaceMethod(vector<string>{tStrNegDec}, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_digit_yi1);
		}
		else if(tStrNegDec=="250" && tVecZhBefore[2]=="是个") {
			tStrReplace += "二百五";
		}
		else if(tStrNegDec=="12345" && tVecZhAfter[2]=="上山") {
			tStrReplace += "壹二三四五";
		}
		else if( (tEnMonth!="" && tStrNegDec.length()==4) || (tVecZhAfter[1]=="届" || tVecZhAfter[2]=="财年" || tVecZhBefore[3]=="总结篇") ) {
			tStrReplace += ReplaceMethod(vector<string>{tStrNegDec}, (fcInLanguage==LANG_EN)?RME_en_year:RME_zh_digit_yi1);
			tStrReplace += " ";
		}
		else if(tStrNegDec[0]=='0') {
			tStrReplace += ReplaceMethod(vector<string>{tStrNegDec}, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_telephone);
			tStrReplace += " ";
		}
		else {
			tStrReplace += ReplaceMethod(vector<string>{tStrNegDec}, (fcInLanguage==LANG_EN)?RME_en_negative_decimal:RME_zh_negative_decimal);
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}
